package com.caifu.mapper;

import com.caifu.pojo.YlWardRoundRecord;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author Lyf
 * @since 2020-11-19
 */
public interface YlWardRoundRecordMapper extends BaseMapper<YlWardRoundRecord> {

    @Select("select info.user_no as user_no,org.org_name, info.birth_date, building.building_name, room.room_name, bed.bed_name, info.user_name, info.native_place, info.age, ncost.cost_name as nursing_cost_name, date_format(contract.start_date,'%y-%m-%d') as start_date from yl_elderly_info info left join yl_bed_record bedrecord on info.user_no = bedrecord.user_no and bedrecord.is_effective ='1' left join yl_building building on bedrecord.building_no = building.building_no left join yl_room room on room.room_no = bedrecord.room_no left join yl_bed bed on bed.bed_no = bedrecord.bed_no left join yl_contract_info contract on contract.user_no = info.user_no left join yl_assessment_record arecord on arecord.user_no = info.user_no and arecord.is_effective ='1' left join yl_cost acost on arecord.cost_no = acost.cost_no left join yl_nursing_record nrecord on nrecord.user_no = info.user_no and nrecord.is_effective ='1' left join yl_cost ncost on nrecord.cost_no = ncost.cost_no left join sys_organization org on info.pension_no = org.org_id where info.user_no = #{userNo} order by contract.start_date limit 0,1")
    Map<String, String> getUserInfo(@Param("userNo") String userNo);

    @Select("select a.in_no,a.in_date,a.sex,a.job,a.work_place,b.label_name as nation,a.history_info,a.address,a.zhu_su,a.medical_history,a.jw_history,a.disease_info,c.temperature,c.pulse,c.breath,c.blood_pressure,c.inspect_describe,d.disease,d.disease_text,d.treatment_planning from yl_healthy_archives_record a left join ys_label b on a.nation = b.label_code left join yl_physique_check_record c on a.batch_no = c.batch_no left join yl_preliminary_diagnosis d on a.batch_no = d.batch_no where a.batch_no = #{batchNo}")
    Map<String, String> getInspectInfo(@Param("batchNo") String batchNo);

    @Select("<script>"+
            " select a.id,e.user_name,date_format(a.create_time,'%Y-%m-%d %H:%i:%s') as create_time,d.user_name as create_by,a.user_no,b.is_effective from yl_ward_round_record a left join yl_bed_record b on a.user_no = b.user_no and b.is_effective ='1' left join yl_building c on b.building_no = c.building_no left join sys_user d on a.create_by = d.user_no left join yl_elderly_info e on a.user_no = e.user_no where a.status = '2'" +
            " <if test='buildingNo != null and buildingNo != &quot;&quot;'>"+
            " and c.building_no = #{buildingNo}"+
            " </if >" +
            " <if test='startDate != null and startDate != &quot;&quot;'>"+
            " and date_format(a.create_time,'%Y%m%d') &gt;= #{startDate}"+
            " </if >" +
            " <if test='endDate != null and endDate != &quot;&quot;'>"+
            " and date_format(a.create_time,'%Y%m%d') &lt;= #{endDate}"+
            " </if >" +
            " <if test='userName != null and userName != &quot;&quot;'>"+
            " and e.user_name like concat('%',#{userName},'%')"+
            " </if >" +
            " order by a.create_time desc,a.user_no limit #{start},#{pageSize}"+
            " </script>")
    List<Map<String,String>> getList(@Param("start") int start,@Param("pageSize") int pageSize,@Param("buildingNo") String buildingNo,@Param("startDate") String startDate,@Param("endDate") String endDate,@Param("userName") String userName);

    @Select("<script>"+
            " select count(*) as count from yl_ward_round_record a left join yl_bed_record b on a.user_no = b.user_no and b.is_effective ='1' left join yl_building c on b.building_no = c.building_no left join sys_user d on a.create_by = d.user_no left join yl_elderly_info e on a.user_no = e.user_no where a.status = '2'" +
            " <if test='buildingNo != null and buildingNo != &quot;&quot;'>"+
            " and c.building_no = #{buildingNo}"+
            " </if >" +
            " <if test='startDate != null and startDate != &quot;&quot;'>"+
            " and date_format(a.create_time,'%Y%m%d') &gt;= #{startDate}"+
            " </if >" +
            " <if test='endDate != null and endDate != &quot;&quot;'>"+
            " and date_format(a.create_time,'%Y%m%d') &lt;= #{endDate}"+
            " </if >" +
            " <if test='userName != null and userName != &quot;&quot;'>"+
            " and e.user_name like concat('%',#{userName},'%')"+
            " </if >" +
            " </script>")
    int getListCount(@Param("buildingNo")String buildingNo,@Param("startDate") String startDate,@Param("endDate") String endDate,@Param("userName") String userName);
}
